package cn.wildfire.chat.kit.conversation.ext.core;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import cn.wildfire.chat.KLog;
import cn.wildfire.chat.kit.conversation.ext.ImageExt;
import cn.wildfire.chat.kit.conversation.ext.ShootExt;
import cn.wildfire.chat.kit.conversation.ext.VoipExt;
import cn.wildfirechat.model.Conversation;

public class ConversationExtManager {
    private static ConversationExtManager instance;
    private List<ConversationExt> conversationExts;

    private ConversationExtManager() {
        conversationExts = new ArrayList<>();
        init();
    }

    public static synchronized ConversationExtManager getInstance() {
        if (instance == null) {
            instance = new ConversationExtManager();
        }
        return instance;
    }

    //ll拓展控件
    private void init() {
        /**
         * modify by HEKE , 2019/10/16 11:57 聊天媒体相关操作，布局样式
         */
        registerExt(ImageExt.class);
        registerExt(ShootExt.class);
//        registerExt(VoipExt.class);
//        registerExt(LocationExt.class);
//        registerExt(FileExt.class);
    }

    public void registerExt(Class<? extends ConversationExt> clazz) {
        Constructor constructor;
        try {
            constructor = clazz.getConstructor();
            ConversationExt ext = (ConversationExt) constructor.newInstance();
            conversationExts.add(ext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void unregisterExt(Class<? extends ConversationExt> clazz) {
        // TODO
    }

    public List<ConversationExt> getConversationExts(Conversation conversation) {
        /**
         * modify by HEKE , 2019/9/3 22:49 TODO 群聊去除音视频选项 HEKE_GROUP_REMOVE_VOIP
         */
        List<ConversationExt> currentExts = new ArrayList<>();
        KLog.e(this.conversationExts.size());
        for (ConversationExt ext : this.conversationExts) {
            if (!ext.filter(conversation)) {
                if (conversation.type == Conversation.ConversationType.Group) {
                    if (!(ext instanceof VoipExt)) {
                        currentExts.add(ext);
                    }
                } else {
                    currentExts.add(ext);
                }
                KLog.e(ext);
            }
        }
        return currentExts;
    }
}
